package com.shrct.businessprocess.project.impl;

import com.shrct.businessprocess.budget.RCBudgetCategory;
import com.shrct.businessprocess.budget.RCCostBreakDown;
import com.shrct.businessprocess.project.RCActualCostReport;
import com.shrct.businessprocess.project.RCProject;
import com.shrct.businessprocess.project.RCProjectCBDSummaryInfo;
import com.shrct.businessprocess.project.RCProjectMoney;
import com.shrct.businessprocess.project.RCProjectStat;
import com.shrct.businessprocess.project.RCProjectSummaryInfo;
import com.shrct.businessprocess.project.proxy.RCProjectStatProxy;
import edu.fudan.mylang.common.NumberExtensions;
import edu.fudan.mylang.pf.BaseModelObject;
import edu.fudan.mylang.pf.IGenericQuery;
import edu.fudan.mylang.pf.IObjectFactory;
import javax.persistence.AssociationOverride;
import javax.persistence.AssociationOverrides;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Table;
import org.eclipse.xtext.xbase.lib.DoubleExtensions;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.InputOutput;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.hibernate.annotations.DiscriminatorOptions;

@Entity
@DiscriminatorValue("RCPROJECTSTAT_TYPE")
@DiscriminatorOptions(force = true)
@DiscriminatorColumn(length = 64)
@Table(name = "rcprojectstat")
public class RCProjectStatImpl extends BaseModelObject implements RCProjectStat {
  public RCProjectStatImpl() {
    super();
  }
  
  @Embedded
  @AttributeOverrides(value = { @AttributeOverride(name = "contractAmount", column = @Column(name = "summaryInfo_contractAmount") ), @AttributeOverride(name = "invoiceAmount", column = @Column(name = "summaryInfo_invoiceAmount") ), @AttributeOverride(name = "incomeAmount", column = @Column(name = "summaryInfo_incomeAmount") ), @AttributeOverride(name = "acceptAmount", column = @Column(name = "summaryInfo_acceptAmount") ), @AttributeOverride(name = "invoiceTax", column = @Column(name = "summaryInfo_invoiceTax") ), @AttributeOverride(name = "actualCost", column = @Column(name = "summaryInfo_actualCost") ), @AttributeOverride(name = "purchaseTax", column = @Column(name = "summaryInfo_purchaseTax") ), @AttributeOverride(name = "totalCost", column = @Column(name = "summaryInfo_totalCost") ), @AttributeOverride(name = "totalCostWithoutTax", column = @Column(name = "summaryInfo_totalCostWithoutTax") ), @AttributeOverride(name = "contractProfit", column = @Column(name = "summaryInfo_contractProfit") ), @AttributeOverride(name = "contractProfitAdj4Sale", column = @Column(name = "summaryInfo_contractProfitAdj4Sale") ), @AttributeOverride(name = "ggp", column = @Column(name = "summaryInfo_ggp") ), @AttributeOverride(name = "cggp", column = @Column(name = "summaryInfo_cggp") ), @AttributeOverride(name = "workaround", column = @Column(name = "summaryInfo_workaround") ) } )
  @AssociationOverrides(value = { } )
  private RCProjectSummaryInfo summaryInfo = com.shrct.businessprocess.project.RCProjectSummaryInfo.create();
  
  public RCProjectSummaryInfo getSummaryInfo() {
    return this.summaryInfo;
  }
  
  public RCProjectStat setSummaryInfo(final RCProjectSummaryInfo summaryInfo) {
    this.summaryInfo = summaryInfo;
    return this;			
    
  }
  
  @Embedded
  @AttributeOverrides(value = { @AttributeOverride(name = "contractAmount", column = @Column(name = "cbdSummaryInfo_contractAmount") ), @AttributeOverride(name = "netPriceAmount", column = @Column(name = "cbdSummaryInfo_netPriceAmount") ), @AttributeOverride(name = "grossProfit", column = @Column(name = "cbdSummaryInfo_grossProfit") ), @AttributeOverride(name = "workaround", column = @Column(name = "cbdSummaryInfo_workaround") ) } )
  @AssociationOverrides(value = { } )
  private RCProjectCBDSummaryInfo cbdSummaryInfo = com.shrct.businessprocess.project.RCProjectCBDSummaryInfo.create();
  
  public RCProjectCBDSummaryInfo getCbdSummaryInfo() {
    return this.cbdSummaryInfo;
  }
  
  public RCProjectStat setCbdSummaryInfo(final RCProjectCBDSummaryInfo cbdSummaryInfo) {
    this.cbdSummaryInfo = cbdSummaryInfo;
    return this;			
    
  }
  
  @Embedded
  @AttributeOverrides(value = { @AttributeOverride(name = "amount", column = @Column(name = "contractPrice_amount") ), @AttributeOverride(name = "workaround", column = @Column(name = "contractPrice_workaround") ) } )
  @AssociationOverrides(value = @AssociationOverride(name = "exchange", joinColumns = @JoinColumn(name = "contractPrice_exchange") ) )
  private RCProjectMoney contractPrice = com.shrct.businessprocess.project.RCProjectMoney.create();
  
  public RCProjectMoney getContractPrice() {
    return this.contractPrice;
  }
  
  public RCProjectStat setContractPrice(final RCProjectMoney contractPrice) {
    this.contractPrice = contractPrice;
    return this;			
    
  }
  
  private Double actualCost;
  
  public Double getActualCost() {
    return this.actualCost;
  }
  
  public RCProjectStat setActualCost(final Double actualCost) {
    this.actualCost = actualCost;
    return this;			
    
  }
  
  private Double grossProfitTarget;
  
  public Double getGrossProfitTarget() {
    return this.grossProfitTarget;
  }
  
  public RCProjectStat setGrossProfitTarget(final Double grossProfitTarget) {
    this.grossProfitTarget = grossProfitTarget;
    return this;			
    
  }
  
  private Double grossProfit4SalesTarget;
  
  public Double getGrossProfit4SalesTarget() {
    return this.grossProfit4SalesTarget;
  }
  
  public RCProjectStat setGrossProfit4SalesTarget(final Double grossProfit4SalesTarget) {
    this.grossProfit4SalesTarget = grossProfit4SalesTarget;
    return this;			
    
  }
  
  private Double grossProfitGGP;
  
  public Double getGrossProfitGGP() {
    return this.grossProfitGGP;
  }
  
  public RCProjectStat setGrossProfitGGP(final Double grossProfitGGP) {
    this.grossProfitGGP = grossProfitGGP;
    return this;			
    
  }
  
  private Double grossProfit4SalesGGP;
  
  public Double getGrossProfit4SalesGGP() {
    return this.grossProfit4SalesGGP;
  }
  
  public RCProjectStat setGrossProfit4SalesGGP(final Double grossProfit4SalesGGP) {
    this.grossProfit4SalesGGP = grossProfit4SalesGGP;
    return this;			
    
  }
  
  private Double partARemain;
  
  public Double getPartARemain() {
    return this.partARemain;
  }
  
  public RCProjectStat setPartARemain(final Double partARemain) {
    this.partARemain = partARemain;
    return this;			
    
  }
  
  private Double partBRemain;
  
  public Double getPartBRemain() {
    return this.partBRemain;
  }
  
  public RCProjectStat setPartBRemain(final Double partBRemain) {
    this.partBRemain = partBRemain;
    return this;			
    
  }
  
  private Double partCRemain;
  
  public Double getPartCRemain() {
    return this.partCRemain;
  }
  
  public RCProjectStat setPartCRemain(final Double partCRemain) {
    this.partCRemain = partCRemain;
    return this;			
    
  }
  
  private Double partDRemain;
  
  public Double getPartDRemain() {
    return this.partDRemain;
  }
  
  public RCProjectStat setPartDRemain(final Double partDRemain) {
    this.partDRemain = partDRemain;
    return this;			
    
  }
  
  private Double partERemain;
  
  public Double getPartERemain() {
    return this.partERemain;
  }
  
  public RCProjectStat setPartERemain(final Double partERemain) {
    this.partERemain = partERemain;
    return this;			
    
  }
  
  private Double partFRemain;
  
  public Double getPartFRemain() {
    return this.partFRemain;
  }
  
  public RCProjectStat setPartFRemain(final Double partFRemain) {
    this.partFRemain = partFRemain;
    return this;			
    
  }
  
  public RCProject getProject() {
    IGenericQuery<RCProject> query=getObjectFactory().createGenericQuery(RCProject.class,"select e from com.shrct.businessprocess.project.impl.RCProjectImpl as e   where ((e.stat) = (:this)) and  e.active = 1      ").setParameter("this",this);
    RCProject _uniqueResult = query.uniqueResult();
    return _uniqueResult;
  }
  
  public void updateContractPrice() {
    RCProject _project = this.getProject();
    RCProjectMoney _totalContractAmount = _project.getTotalContractAmount();
    this.contractPrice = _totalContractAmount;
  }
  
  public void updateInfo() {
    try {
      final RCProject project = this.getProject();
      final RCActualCostReport report = project.getTempActualCostReport();
      RCCostBreakDown _cbd = project.getCbd();
      RCProjectMoney _salesActualExCost = _cbd.getSalesActualExCost();
      Double _hostAmount = _salesActualExCost.toHostAmount();
      final Double salesExCost = NumberExtensions.nullsafe(_hostAmount);
      RCProjectMoney _totalContractAmount = project.getTotalContractAmount();
      this.contractPrice = _totalContractAmount;
      Double _totalActualAmount = report.getTotalActualAmount();
      Double _nullsafe = NumberExtensions.nullsafe(_totalActualAmount);
      this.actualCost = _nullsafe;
      RCCostBreakDown _cbd_1 = project.getCbd();
      RCProjectMoney _grossProfit = _cbd_1.getGrossProfit();
      Double _hostAmount_1 = _grossProfit.toHostAmount();
      Double _nullsafe_1 = NumberExtensions.nullsafe(_hostAmount_1);
      this.grossProfitTarget = _nullsafe_1;
      double _minus = DoubleExtensions.operator_minus(this.grossProfitTarget, salesExCost);
      this.grossProfit4SalesTarget = Double.valueOf(_minus);
      RCProjectMoney _totalInvoiceAmount = project.getTotalInvoiceAmount();
      Double _hostAmount_2 = _totalInvoiceAmount.toHostAmount();
      Double _nullsafe_2 = NumberExtensions.nullsafe(_hostAmount_2);
      double _minus_1 = DoubleExtensions.operator_minus(_nullsafe_2, this.actualCost);
      this.grossProfitGGP = Double.valueOf(_minus_1);
      double _minus_2 = DoubleExtensions.operator_minus(this.grossProfitGGP, salesExCost);
      this.grossProfit4SalesGGP = Double.valueOf(_minus_2);
      RCCostBreakDown _cbd_2 = project.getCbd();
      Double _remainAmount = _cbd_2.getRemainAmount(RCBudgetCategory.PartA);
      this.partARemain = _remainAmount;
      RCCostBreakDown _cbd_3 = project.getCbd();
      Double _remainAmount_1 = _cbd_3.getRemainAmount(RCBudgetCategory.PartB);
      this.partBRemain = _remainAmount_1;
      RCCostBreakDown _cbd_4 = project.getCbd();
      Double _remainAmount_2 = _cbd_4.getRemainAmount(RCBudgetCategory.PartC);
      this.partCRemain = _remainAmount_2;
      RCCostBreakDown _cbd_5 = project.getCbd();
      Double _remainAmount_3 = _cbd_5.getRemainAmount(RCBudgetCategory.PartD);
      this.partDRemain = _remainAmount_3;
      RCCostBreakDown _cbd_6 = project.getCbd();
      Double _remainAmount_4 = _cbd_6.getRemainAmount(RCBudgetCategory.PartE);
      this.partERemain = _remainAmount_4;
      RCCostBreakDown _cbd_7 = project.getCbd();
      Double _remainAmount_5 = _cbd_7.getRemainAmount(RCBudgetCategory.PartF);
      this.partFRemain = _remainAmount_5;
      this.summaryInfo.performUpdate(project, report);
      this.cbdSummaryInfo.performUpdate(project);getObjectFactory().delete(report);
      
    } catch (final Throwable _t) {
      if (_t instanceof Exception) {
        final Exception e = (Exception)_t;
        e.printStackTrace();
        InputOutput.<String>println("update info failed");
        RuntimeException _runtimeException = new RuntimeException(e);
        throw _runtimeException;
      } else {
        throw Exceptions.sneakyThrow(_t);
      }
    }
  }
  
  public static RCProjectStat create(final IObjectFactory objectFactory) {
    com.shrct.businessprocess.project.RCProjectStat rCProjectStat = new com.shrct.businessprocess.project.impl.RCProjectStatImpl(
    );
    objectFactory.create(rCProjectStat);
    return rCProjectStat;			
    
  }
  
  public static RCProjectStat createRCProjectStatImpl(final IObjectFactory objectFactory, final Procedure1<RCProjectStat> init) {
    com.shrct.businessprocess.project.RCProjectStat rCProjectStat = new com.shrct.businessprocess.project.impl.RCProjectStatImpl(
    );
    objectFactory.create(rCProjectStat);
    init.apply(rCProjectStat);
    
    return rCProjectStat;			
    
  }
  
  public RCProjectStatProxy toProxy() {
    com.shrct.businessprocess.project.proxy.RCProjectStatProxy proxy = new com.shrct.businessprocess.project.proxy.RCProjectStatProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
